/**
 * Created by guomin on 2017/5/4.
 */
var Promise=require('bluebird');
var validator=require('validator');

var checkParams={
    //参数是否完整
    isDefined:function(params,filedNames){
        return new Promise(function(resolve,reject){
            for(var i=0;i<filedNames.length;i++){
                if(!(filedNames[i] in params)){
                    reject(101);
                }
            }
            resolve(true);
        });
    },
    //参数是否是非负整数
    isRightFormat:function(params,filedNames){
        return new Promise(function(resolve,reject){
            for(var i=0;i<filedNames.length;i++){
                var filed=filedNames[i];
                var values=params[filed].toString();
                if(!validator.isInt(values)||values<0){
                    reject(102);
                }
            }
            resolve(true);
        });
    },
    //参数是否为空
    isParamsEmpty:function(params,filedNames){
        return new Promise(function(resolve,reject){
            for(var i=0;i<filedNames.length;i++){
                var filed=filedNames[i];
                if(params[filed].length==0){
                    reject(105);
                }
            }
            resolve(true);
        });
    },
    //手机号码格式验证
    isMobile:function(mobile){
        return new Promise(function(resolve,reject){
            var regMobile=/^0?1[3|4|5|7|8]\d{9}$/;
            if(regMobile.test(mobile)){
                resolve(true);
            }else{
                reject(104);
            }
        });
    },
    //图片验证码是否正确
    isRightCaptcha:function(req,captcha){
        return new Promise(function(resolve,reject){
            console.log("client captcha",captcha);
            console.log("sessionID captcha",req.sessionID);
            if(req.session.captcha==undefined){
                reject(103);
            }else{
                if(req.session.captcha.toLowerCase()==captcha.toLowerCase()){
                    resolve(true);
                }else{
                    reject(103);
                }
            }
        });
    },
    //验证同一手机号60s内是否重复发送
    checkSameMobile:function(mobile,client){
        return new Promise(function(resolve,reject){
            client.get(mobile,function(err,reply){
                if(err){
                    reject(err);
                }
                if(reply){
                    reject(106);
                }else{
                    resolve(true);
                }
            });
        });
    },
    //验证手机短信验证码是否有效
    smsIsValid:function(params,client){
        return new Promise(function(resolve,reject){
            var mobile=params.mobile;
            var smsCode=params.smsCode;
            var key=mobile+"str";
            client.get(key,function(err,reply){
                console.log(key+":"+reply);
                if(err){
                    reject(err);
                }else{
                    if(reply){
                        if(reply==smsCode){
                            resolve(true);
                        }else{
                            reject(108);
                        }
                    }else{
                        reject(107);
                    }
                }
            });
        });
    }
};

exports=module.exports=checkParams;